%{
open Printf
open Ast
%}

%token <string> WORD
%token <string> LT_ID
%token <string> LT_SLASH_ID_GT
%token <string> ID_SP_EQ_SP
%token <string> STRING
%token <string> COMMENT

%token EQ SLASH GT SLASH_GT
%token EOF

%start prog
%type <Ast.program> prog

%% 

prog:
  | node_list_opt           { $1 }

element:
  | comment_opt LT_ID attr_list_opt GT node_list_opt LT_SLASH_ID_GT { ElementNode {comment=$1; tagname=$2; attributes=$3; children=$5} }
  | comment_opt LT_ID attr_list_opt SLASH_GT                        { ElementNode {comment=$1; tagname=$2; attributes=$3; children=[]} }


comment_opt:
  |               { None    }
  | COMMENT       { Some $1 }

node_list_opt:
  |                         { []        }
  | text                    { [$1]      }
  | text element            { [$1; $2]  }
  | element node_list_opt   { $1 :: $2  }

attr_list_opt:
  |                         { []        }
  | attr attr_list_opt      { $1 :: $2  }

attr:
  | WORD                    { ($1, None) }
  | ID_SP_EQ_SP rval        { ($1, Some $2)        }

rval:
  | WORD                    { $1 }
  | STRING                  { $1 }

text: 
  | word_list               { TextNode $1 }

word_list:
  | WORD                    { [$1] } 
  | WORD word_list          { $1 :: $2 }










